import { getCategory } from "../../request/category";

export interface Item {
  cat_deleted: boolean;
  cat_icon: string;
  cat_id: number;
  cat_level: number;
  cat_name: string;
  cat_pid: number;
}

export interface Category_Child {
  cat_deleted: boolean;
  cat_icon: string;
  cat_id: number;
  cat_level: number;
  cat_name: string;
  cat_pid: number;
  children: Array<Item>;
}

interface Category {
  cat_deleted: boolean;
  cat_icon: string;
  cat_id: number;
  cat_level: number;
  cat_name: number;
  cat_pid: number;
  children: Array<Category_Child>;
}

let category: Array<Category> = [];
let activeId = 0;
let currentTab: Array<Category_Child> = [];
Page({
  data: {
    category,
    activeId,
  },
  changeActiveId(e: any) {
    const id = e.currentTarget.dataset.id as number;
    activeId = id;
    currentTab = category.find((item) => item.cat_id === id)?.children || [];
    this.initTab();
    this.setData({ activeId });
  },
  onLoad() {
    this.initCategory();
  },
  async initCategory() {
    const result = await getCategory();
    category = result as any;
    activeId = category[0].cat_id;
    currentTab = category[0].children;
    this.initTab();
    this.setData({ category, activeId });
  },
  initTab() {
    const tab = this.selectComponent("#tab");
    tab.data.tab = currentTab;
    tab.setData({
      tab: currentTab,
      currentItem: currentTab[0].children,
      activeId: currentTab[0].cat_id,
      cat_id: activeId,
    });
  },
});
